/**
 * Author: Balaram Putta
 * Date: Dec 16, 2003
 * Time: 3:34:04 PM
 */
package com.scalar.chiptrack.tags;

import java.util.HashMap;
import java.util.LinkedHashMap;

import org.apache.commons.digester.Digester;

import com.scalar.chiptrack.utils.ConfigManager;

/**
 * Class to parse the site_map.xml file.
 *
 * @author Balaram Putta
 * @version
 */
public class SiteMapConfig
{
    // Patterns to look for in the site map config file
    public static final String PATTERN_PAGE_TAG = "urh/page";
    public static final String PATTERN_STEP_TAG = "urh/page/step";

    // HashMap to store Steps againt Page names
    private static HashMap sPageStepMap = new LinkedHashMap();

    // HashMap to store Step name and link values
    private HashMap mStepNameValueMap = new LinkedHashMap();

    // One and only object to used by this singleton
    private static SiteMapConfig sSiteMapConfig = null;

    // Flag that identifies whether this class is initialized or not
    private static boolean sInitialized = false;


    // Static initialization part
    static
    {
        init();
    }

    // Private Constructor
    private SiteMapConfig(){}


    /**
     * Initialization method. Parses the mapping file and saves
     * the data into member variables.
     */
    public static void init()
    {
        if( sInitialized == false )
        {
            try
            {
                if( sSiteMapConfig == null )
                {
                    sSiteMapConfig = new SiteMapConfig();

                }

                Digester digester = new Digester();
                digester.push( sSiteMapConfig );

                digester.addCallMethod( PATTERN_PAGE_TAG, "addPageMap", 1);
                digester.addCallParam( PATTERN_PAGE_TAG, 0, "name" );

                digester.addCallMethod( PATTERN_STEP_TAG, "addStepsToPageMap", 2 );
                digester.addCallParam( PATTERN_STEP_TAG, 0, "name" );
                digester.addCallParam( PATTERN_STEP_TAG, 1, "link" );

                String mappingFileLocation = ConfigManager.getSiteMapConfigFile();  //"c:/integ/config/site_map.xml";
                if( ( mappingFileLocation == null ) || ( mappingFileLocation.trim().length() == 0 ) )
                {
                    System.out.println ( "******* ERRPR Invalid Sitemap configuration file location, please check saclar.properties" );
                    sInitialized = false;
                }
                else
                {
                    digester.parse( mappingFileLocation ); // "c:/integ/config/site_map.xml"
                    sInitialized = true;
                }
            }
            catch( Throwable t )
            {
                System.out.println( "******** ERROR: Could not load Sitemap configuration file" );
                t.printStackTrace();
                sInitialized = false;
            }
        }
    }


    /**
     *
     * @param pageName
     * @return
     */
    public static HashMap getStepsByPage( String pageName )
    {
        HashMap stepsMap = new HashMap();

        if( ( sInitialized == true ) && ( sPageStepMap != null ) && ( pageName != null ) )
        {
            stepsMap = ( HashMap ) sPageStepMap.get( pageName );
        }

        return stepsMap;
    }

    // Tests this class
    public static void main( String[] args ) throws Exception
    {
        System.out.println( SiteMapConfig.getStepsByPage( "payment_term_search.jsp" ) );
    }

    /*-------------------- DIGESTER CALL BACK METHODS ------------ START -----------------------------------*/

    /**
     *
     * @param pageName
     */
    public void addPageMap( String pageName )
    {
        //System.out.println( "pageName = " + pageName );

        sPageStepMap.put( pageName, mStepNameValueMap );
        mStepNameValueMap  = new LinkedHashMap();
    }

    /**
     * Adds an entry to the stepNameValue Map.
     * @param name
     * @param link
     */
    public void addStepsToPageMap( String name, String link )
    {
        //System.out.println( "key = " + name + ", value = " + link );
        mStepNameValueMap.put( name, link );
    }

    /*-------------------- DIGESTER CALL BACK METHODS ------------- END -----------------------------------*/

}
